package com.icesoft.generation.helper;

import com.icesoft.framework.core.util.StringUtils;
import com.icesoft.generation.vo.GenTableVO;
import org.apache.velocity.VelocityContext;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class VelocityHelper {

	public static VelocityContext prepareContext(GenTableVO genTable) {
		VelocityContext velocityContext = new VelocityContext();
		velocityContext.put("packageName", genTable.getPackageName());
		velocityContext.put("tableName", genTable.getTableName());
		velocityContext.put("tableComment", genTable.getTableComment());
		velocityContext.put("moduleName", genTable.getModuleName());
		velocityContext.put("className", genTable.getClassName());
		velocityContext.put("requestMapping", genTable.getRequestMapping());
		velocityContext.put("fields", genTable.getFields());
		velocityContext.put("dtoFields", genTable.getDtoFields());
		velocityContext.put("voFields", genTable.getVoFields());

		velocityContext.put("javaPath", genTable.getJavaPath());
		velocityContext.put("vuePagePath", genTable.getVuePagePath());
		velocityContext.put("vuePageName", genTable.getVuePageName());
		velocityContext.put("vueApiPath", genTable.getVueApiPath());
		velocityContext.put("vueApiName", genTable.getVueApiName());
		String classNameSmallLetter = genTable.getClassName().substring(0, 1).toLowerCase() + genTable.getClassName().substring(1);
		velocityContext.put("classNameSmallLetter", classNameSmallLetter);
		return velocityContext;
	}

	public static List<String> getAllTemplateList() {
		List<String> templates = new ArrayList<>();
		templates.add("vm/java/entity.java.vm");
		templates.add("vm/java/vo.java.vm");
		templates.add("vm/java/dto.java.vm");
		templates.add("vm/java/controller.java.vm");
		templates.add("vm/java/mapper.java.vm");
		templates.add("vm/java/service.java.vm");
		templates.add("vm/java/serviceImpl.java.vm");
		templates.add("vm/vue/Create.vue.vm");
		templates.add("vm/vue/Edit.vue.vm");
		templates.add("vm/vue/List.vue.vm");
		templates.add("vm/vue/Detail.vue.vm");
		templates.add("vm/vue/api/api.ts.vm");
		templates.add("vm/vue/api/types.ts.vm");
		return templates;
	}

	public static List<String> getTemplateList() {
		List<String> templates = new ArrayList<>();
		templates.add("vm/java/entity.java.vm");
		templates.add("vm/java/vo.java.vm");
		templates.add("vm/java/dto.java.vm");
		templates.add("vm/java/controller.java.vm");
		templates.add("vm/java/mapper.java.vm");
		templates.add("vm/java/service.java.vm");
		templates.add("vm/java/serviceImpl.java.vm");
		return templates;
	}

	public static List<String> getVueTemplateList() {
		List<String> templates = new ArrayList<>();
		templates.add("vm/vue/Create.vue.vm");
		templates.add("vm/vue/Edit.vue.vm");
		templates.add("vm/vue/List.vue.vm");
		templates.add("vm/vue/Detail.vue.vm");
		templates.add("vm/vue/api/api.ts.vm");
		templates.add("vm/vue/api/types.ts.vm");
		return templates;
	}

	public static String getAllFileName(String template, GenTableVO genTable) throws IOException {
		// 文件名称
		String fileName = "";
		String projectName = new ClassPathResource("").getFile().getAbsolutePath();
		projectName = projectName.replace("target\\classes", "").replace("\\", "/");
		String java_path = projectName + "/src/main/java/" + genTable.getPackageName().replace(".", "/") + "/";
		String classNameSmallLetter = genTable.getClassName().substring(0, 1).toLowerCase() + genTable.getClassName().substring(1);
		String projectRootDirectoryPath = System.getProperty("user.dir");
		String vueProjectName = projectRootDirectoryPath + "/element-admin-ui";

		if (template.contains("controller.java.vm")) {
			fileName = StringUtils.format("{}controller/{}Controller.java", java_path, genTable.getClassName());
		} else if (template.contains("dto.java.vm")) {
			fileName = StringUtils.format("{}dto/{}DTO.java", java_path, genTable.getClassName());
		} else if (template.contains("entity.java.vm")) {
			fileName = StringUtils.format("{}entity/{}.java", java_path, genTable.getClassName());
		} else if (template.contains("mapper.java.vm")) {
			fileName = StringUtils.format("{}mapper/{}Mapper.java", java_path, genTable.getClassName());
		} else if (template.contains("service.java.vm")) {
			fileName = StringUtils.format("{}service/{}Service.java", java_path, genTable.getClassName());
		} else if (template.contains("serviceImpl.java.vm")) {
			fileName = StringUtils.format("{}service/impl/{}ServiceImpl.java", java_path, genTable.getClassName());
		} else if (template.contains("vo.java.vm")) {
			fileName = StringUtils.format("{}vo/{}VO.java", java_path, genTable.getClassName());
		} else if (template.contains("Create.vue.vm")) {
			fileName = StringUtils.format("{}/src/views/{}/{}/Create.vue", vueProjectName, genTable.getModuleName(), classNameSmallLetter);
		} else if (template.contains("Edit.vue.vm")) {
			fileName = StringUtils.format("{}/src/views/{}/{}/Edit.vue", vueProjectName, genTable.getModuleName(), classNameSmallLetter);
		} else if (template.contains("Detail.vue.vm")) {
			fileName = StringUtils.format("{}/src/views/{}/{}/Detail.vue", vueProjectName, genTable.getModuleName(), classNameSmallLetter);
		} else if (template.contains("List.vue.vm")) {
			fileName = StringUtils.format("{}/src/views/{}/{}/List.vue", vueProjectName, genTable.getModuleName(), classNameSmallLetter);
		} else if (template.contains("api.ts.vm")) {
			fileName = StringUtils.format("{}/src/api/{}/{}/{}.api.ts", vueProjectName, genTable.getModuleName(), classNameSmallLetter, classNameSmallLetter);
		} else if (template.contains("types.ts.vm")) {
			fileName = StringUtils.format("{}/src/api/{}/{}/types.ts", vueProjectName, genTable.getModuleName(), classNameSmallLetter, classNameSmallLetter);
		}
		return fileName;
	}


	public static String getFileName(String template, GenTableVO genTable) throws IOException {
		// 文件名称
		String fileName = "";
		String projectName = new ClassPathResource("").getFile().getAbsolutePath();
		projectName = projectName.replace("target\\classes", "").replace("\\", "/");
		String java_path = projectName + "/src/main/java/" + genTable.getPackageName().replace(".", "/") + "/";

		if (template.contains("controller.java.vm")) {
			fileName = StringUtils.format("{}controller/{}Controller.java", java_path, genTable.getClassName());
		} else if (template.contains("dto.java.vm")) {
			fileName = StringUtils.format("{}dto/{}DTO.java", java_path, genTable.getClassName());
		} else if (template.contains("entity.java.vm")) {
			fileName = StringUtils.format("{}entity/{}.java", java_path, genTable.getClassName());
		} else if (template.contains("mapper.java.vm")) {
			fileName = StringUtils.format("{}mapper/{}Mapper.java", java_path, genTable.getClassName());
		} else if (template.contains("service.java.vm")) {
			fileName = StringUtils.format("{}service/{}Service.java", java_path, genTable.getClassName());
		} else if (template.contains("serviceImpl.java.vm")) {
			fileName = StringUtils.format("{}service/impl/{}ServiceImpl.java", java_path, genTable.getClassName());
		} else if (template.contains("vo.java.vm")) {
			fileName = StringUtils.format("{}vo/{}VO.java", java_path, genTable.getClassName());
		}
		return fileName;
	}

	public static String getVueFileName(String template, GenTableVO genTable) {
		// 文件名称
		String fileName = "";
		String projectRootDirectoryPath = System.getProperty("user.dir");
		String vueProjectName = projectRootDirectoryPath + "/element-admin-ui";
		String classNameSmallLetter = genTable.getClassName().substring(0, 1).toLowerCase() + genTable.getClassName().substring(1);

		if (template.contains("Create.vue.vm")) {
			fileName = StringUtils.format("{}/src/views/{}/{}/Create.vue", vueProjectName, genTable.getModuleName(), classNameSmallLetter);
		} else if (template.contains("Edit.vue.vm")) {
			fileName = StringUtils.format("{}/src/views/{}/{}/Edit.vue", vueProjectName, genTable.getModuleName(), classNameSmallLetter);
		} else if (template.contains("Detail.vue.vm")) {
			fileName = StringUtils.format("{}/src/views/{}/{}/Detail.vue", vueProjectName, genTable.getModuleName(), classNameSmallLetter);
		} else if (template.contains("List.vue.vm")) {
			fileName = StringUtils.format("{}/src/views/{}/{}/List.vue", vueProjectName, genTable.getModuleName(), classNameSmallLetter);
		} else if (template.contains("api.ts.vm")) {
			fileName = StringUtils.format("{}/src/api/{}/{}/{}.api.ts", vueProjectName, genTable.getModuleName(), classNameSmallLetter, classNameSmallLetter);
		} else if (template.contains("types.ts.vm")) {
			fileName = StringUtils.format("{}/src/api/{}/{}/types.ts", vueProjectName, genTable.getModuleName(), classNameSmallLetter, classNameSmallLetter);
		}
		return fileName;
	}

}
